{
  "properties" : { },
  "id" : "a8c942fe51d14b2fa71d9bb307ac410f",
  "script" : null,
  "groupId" : "ae84b87a26724813a446a132163ff3ca",
  "name" : "登录注册",
  "createTime" : null,
  "updateTime" : 1733191937899,
  "lock" : null,
  "createBy" : null,
  "updateBy" : null,
  "path" : "/login",
  "method" : "POST",
  "parameters" : [ {
    "name" : "type",
    "value" : "login",
    "description" : null,
    "required" : false,
    "dataType" : "String",
    "type" : null,
    "defaultValue" : "login",
    "validateType" : null,
    "error" : null,
    "expression" : null,
    "children" : null
  }, {
    "name" : "username",
    "value" : "test12324",
    "description" : null,
    "required" : false,
    "dataType" : "String",
    "type" : null,
    "defaultValue" : null,
    "validateType" : null,
    "error" : null,
    "expression" : null,
    "children" : null
  }, {
    "name" : "password",
    "value" : "123456",
    "description" : null,
    "required" : false,
    "dataType" : "String",
    "type" : null,
    "defaultValue" : null,
    "validateType" : null,
    "error" : null,
    "expression" : null,
    "children" : null
  } ],
  "options" : [ {
    "name" : "require_login",
    "value" : "false",
    "description" : "该接口需要登录才允许访问",
    "required" : false,
    "dataType" : "String",
    "type" : null,
    "defaultValue" : null,
    "validateType" : null,
    "error" : null,
    "expression" : null,
    "children" : null
  } ],
  "requestBody" : "",
  "headers" : [ ],
  "paths" : [ ],
  "responseBody" : "{\n    \"code\": 200,\n    \"message\": \"success\",\n    \"data\": {\n        \"id\": 6,\n        \"username\": \"test12324\",\n        \"token\": \"919bce0c-9ca2-4746-8f45-51e4aa2620b7\"\n    },\n    \"timestamp\": 1733154602085,\n    \"executeTime\": 46\n}",
  "description" : null,
  "requestBodyDefinition" : null,
  "responseBodyDefinition" : null
}
================================
import cn.hutool.core.util.StrUtil
import cn.hutool.core.util.RandomUtil
import cn.hutool.core.util.ReUtil;
import log
import 'cn.dev33.satoken.secure.SaSecureUtil';
import 'cn.dev33.satoken.stp.StpUtil';
import 'cn.hutool.core.lang.Validator';
import 'cn.hutool.core.util.StrUtil';
import env;
import request;
import http;

var username = body.username
var password = body.password
var type = body.type

var user

if (type == 'reg') {
    userInfo = db.table("users").where().eq("username", username).selectOne()
    if (userInfo != null) {
        exit 400, '用户已存在';
    }
    encrypt = RandomUtil.randomString(6)
    pwd = SaSecureUtil.md5(SaSecureUtil.md5(password) + encrypt)
    db_res = db.table("users").insert({
        encrypt: encrypt,
        username: username,
        password: pwd,
    })
    return {
        id: db_res,
        username
    }
} else {
    userInfo = db.table("users").where().eq("username", username).selectOne()

    if (userInfo == null) {
        exit 400, '用户不存在';
    }

    log.info("userInfo-->"+userInfo)
    log.info("userInfo id-->"+userInfo.id)
    pwd = SaSecureUtil.md5(SaSecureUtil.md5(password) + userInfo.encrypt)
    if (userInfo.password != pwd) {
        exit 0, '用户名或密码错误'
    }
    //更新用户id和最后登录时间

    var data = {
        id,
        login_num,
        last_ip,
        last_at,
        line_ip,
        line_at,
    }

    ip = request.getClientIP()
    data.id = userInfo.id
    data.login_num = (userInfo.loginNum ? userInfo.loginNum : 0) + 1
    data.last_ip = ip
    data.last_at = now()
    data.line_ip = ip
    data.line_at = now()

    db.table('users').primary('id').update(data, true)
    //执行登录
    StpUtil.login(userInfo.id)
    var token = StpUtil.getTokenValueByLoginId(userInfo.id)
    userInfo.token = token
    userInfo.password = ""

    return {
        id:userInfo.id,
        username,
        token
    }
}